{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8cf235de-fa13-4a79-823f-c26681e3f646",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from datetime import datetime\n",
    "import os\n",
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "7f1ee28a-9a15-4110-92ca-d6df83063fa6",
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "目前的思路：\n",
    "    1. 重要与紧急程度的表示：\n",
    "    正半轴：1 - 5\n",
    "    负半轴：-5 - -1\n",
    "    x轴表示重要程度，y轴表示紧急程度\n",
    "\n",
    "    值得一提的是，真正重要的，往往是重要且不紧急的\n",
    "\"\"\"\n",
    "# 需要添加方法，完成任务，修改任务内容，添加任务\n",
    "import pandas as pd\n",
    "\n",
    "class MissionList:\n",
    "    def __init__(self, path=r'MissionList.csv', read_json=False, read_csv=False):\n",
    "        if read_json:\n",
    "            self.list = self.read_json()\n",
    "        elif read_csv:\n",
    "            self.list = self.read_csv(path)\n",
    "        else:\n",
    "            self.list = self.init_list()\n",
    "        self.address = 'D:/MissionList/Misssons/'\n",
    "\n",
    "    def read_json(self, path):\n",
    "        pass\n",
    "\n",
    "    def read_csv(self, path):\n",
    "        try:\n",
    "            df = pd.read_csv(path, encoding='utf-8')\n",
    "            sorted_df = df.sort_values(by=['total', 'importance'], ascending=[False, False])\n",
    "            return sorted_df\n",
    "        except Exception as e:\n",
    "            print(e)\n",
    "\n",
    "    def save_csv(self):\n",
    "        try:\n",
    "            # 读取 mission_count\n",
    "            with open(r'D:\\MissionList\\Misssons\\control.txt', 'r') as f:\n",
    "                line = f.readline()\n",
    "                match = re.search(r'mission_count = (\\d+);', line)\n",
    "                mission_count = int(match.group(1))\n",
    "                \n",
    "            # 更新 mission_count\n",
    "            with open(r'D:\\MissionList\\Misssons\\control.txt', 'w') as f:\n",
    "                newline = f'mission_count = {str(mission_count + 1)};'\n",
    "                f.write(newline)\n",
    "                \n",
    "            #创建文件夹\n",
    "            path = os.path.join(self.address, f'MissionList{match.group(1)}')\n",
    "            os.makedirs(path, exist_ok=True)\n",
    "            current_time = datetime.now().strftime(\"%Y-%m-%d %H时%M分.csv\")\n",
    "            self.list.to_csv(os.path.join(path, current_time), encoding='utf-8', index = False)\n",
    "        except Exception as e:\n",
    "            print(e)\n",
    "\n",
    "    def get_num(self, flag):\n",
    "        name = '重要性' if flag else '紧急性'\n",
    "        while True:\n",
    "            try:\n",
    "                num = int(input(f'{name}(-5-5):'))\n",
    "                if num not in range(-5, 6):\n",
    "                    print('重新输入')\n",
    "                    continue\n",
    "                return num\n",
    "            except Exception as e:\n",
    "                print(e, '\\n重新输入')\n",
    "                continue\n",
    "\n",
    "    def init_list(self):\n",
    "        missions = []\n",
    "        count = 1\n",
    "        while True:\n",
    "            print('任务', count)\n",
    "            mission = input('任务(0 to stop):')\n",
    "            if mission == '0':\n",
    "                break\n",
    "            urgency = self.get_num(0)\n",
    "            importance = self.get_num(1)\n",
    "            total = urgency + importance\n",
    "            missions.append({'mission': mission, 'importance': importance, 'urgency': urgency, 'total': total})\n",
    "            count += 1\n",
    "        df = pd.DataFrame(missions)\n",
    "        sorted_df = df.sort_values(by=['total', 'importance'], ascending=[False, False])\n",
    "        print('任务清单已创建')\n",
    "        return sorted_df\n",
    "\n",
    "    # def show(self):\n",
    "    #     print(f'{\"任务\".ljust(6)} | {\"任务名称\".ljust(25)} | {\"重要性\".ljust(8)} | {\"紧急性\".ljust(8)}')\n",
    "    #     for index, mission in self.list.iterrows():\n",
    "    #         print(f'{str(index+1).ljust(6)} | {mission[\"mission\"].ljust(25)} | {str(mission[\"importance\"]).ljust(8)} | {str(mission[\"urgency\"]).ljust(8)}')\n",
    "    # 问题: 打印始终无法对齐？？？    \n",
    "    def finish_mission(self):\n",
    "        try:\n",
    "            self.show()\n",
    "            index = int(input('所完成任务的编号'))\n",
    "            if index not in self.list.index.tolist():\n",
    "                raise ValueError('编号不存在')\n",
    "            self.list.drop(index,inplace=True)\n",
    "            print('任务', index, '已完成')\n",
    "            self.save_csv()\n",
    "            return True\n",
    "        except Exception as e:\n",
    "            print(e)\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "ef041e60-6426-4c86-a437-e345b3b1e113",
   "metadata": {},
   "outputs": [],
   "source": [
    "path = 'D:/MissionList/Misssons/2025-11-10 20 33.csv'\n",
    "list = MissionList(path, read_csv=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "286bef5b-50cb-4429-8c46-dae2c1d8d555",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mission</th>\n",
       "      <th>importance</th>\n",
       "      <th>urgency</th>\n",
       "      <th>total</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>比特学习一节课</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>数据结构继续学习</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>导论project中期任务\\t</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           mission  importance  urgency  total\n",
       "0          比特学习一节课           5        3      8\n",
       "1         数据结构继续学习           5        2      7\n",
       "2  导论project中期任务\\t           4        3      7"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list.list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "e4759d84-f709-48c1-b9f6-a353b3b879a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "list.save_csv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c8051655-9d05-4541-a87d-8114dd8c261c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:miniconda3]",
   "language": "python",
   "name": "conda-env-miniconda3-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
